﻿using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using SdmxMl.Helper;
using SdmxMl.Common;

namespace SdmxMl.Structure
{

    /// <summary>
    /// ProcessType generically describes a statistical process.
    /// In this version of the SDMX Technical Specifications, it is not meant to
    /// support process automation, but serves as a description of how processes
    /// occur. The primary use for this structural mechanism is the attachment of
    /// reference metadata regarding statistical processing. A process has a
    /// human-readable Name, which may be provided in multiple, parallel-language
    /// versions. It also has an optional human-readable Description, which also
    /// may be provided with multiple, parallel-language versions. The Annotations
    /// element allows for it to be annotated. The id attribute takes a unique id
    /// within the set of processes maintained by the agency identified in the
    /// agencyID attribute. The version attribute indicated the version of teh
    /// process description. The uri value is a URL where a complete description
    /// of the Process may be found; the urn attribute takes the valid registry URN
    /// of the Process, as described in the SDMX Registry Specification. If isFinal
    /// is set to true, the process description cannot be changed without versioning.
    /// If isExternalReference is true, then only the id, agency, Name, and uri
    /// (or URN) need be supplied - all other fields are assumed to be found in
    /// a valid SDMX Structure message found at the uri attribute location. The
    /// validFrom and validTo attributes provide inclusive dates for providing
    /// supplemental validity information about the version.
    /// </summary>
    public class ProcessType : Artefact
    {

        #region References

        public override ArtefactRefTypedList ArtefactRefList
        {
            get
            {
                ArtefactRefTypedList list = new ArtefactRefTypedList();

                return list;
            }
        }


        #endregion

        #region Properties

        private ProcessStepTypeList _processStepList;
        public ProcessStepTypeList ProcessStepList
        {
            get { return _processStepList; }
        }
       
        #endregion //Properties

        #region Constructors
        
        private void Init()
        {
            _processStepList = new ProcessStepTypeList(null);
        }


        public ProcessType()
        {
            Init();
        }


        public ProcessType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr) 
            : base(nodeArtefact, nsmgr)
        {
            Init();
            string query = "./structure:ProcessStep";
            XmlNodeList nodeList = nodeArtefact.SelectNodes(query, nsmgr);
            if (nodeList.Count > 0)
            {
                foreach (XmlNode nd in nodeList)
                {
                    ProcessStepType step = new ProcessStepType(ProcessStepList, null, nd, nsmgr);
                    ProcessStepList.Add(step);
                }
            }
        }

        #endregion // Constructors

        #region XML serialize

        public override void WriteXmlInsider(XmlTextWriter xw)
        {
            string prefix = "structure";
            foreach (ProcessStepType cat in ProcessStepList)
                cat.WriteXml(xw, prefix, "ProcessStep");

        }

        #endregion

        #region Tree

        public override TreeNode FillNode(TreeNode parentNode)
        {
            TreeNode t = base.FillNode(parentNode);
            TreeNode tc = new TreeNode(ProcessStepList.ToString(), 1, 1);
            tc.Tag = ProcessStepList;
            t.Nodes.Add(tc);
            foreach (ProcessStepType s in ProcessStepList)
                s.FillNode(tc);
            return t;
        }

        #endregion // Tree

        #region Artefact Type

        public override Manager.SdmxArtefactType ArType
        {
            get { return Manager.SdmxArtefactType.Processes; }
        }

        #endregion

    }

}
